<!-- 我的娃娃币 -->
<template>
  <div class="my-coin" :class="{nocoinbg: coin == 0}">
      <div class="acount">
      <p class="text">金币余额</p>
      <p class="num">{{coin}}</p>
      <a class="go-charge" href="javascript:;" @touchend="showModal">去充值 ></a>
    </div>
    <div class="consume-list" v-infinite-scroll="getMore" infinite-scroll-disabled="nonext" infinite-scroll-distance="10"
         infinite-scroll-immediate-check="false">
      <div class="consume-item flex" v-for="(item,index) in list" :key="index">
        <div>
          <p class="content" v-if="item.recordable_type=='game_record'">游戏消费</p>
          <p class="content" v-if="item.recordable_type=='system'">系统赠送</p>
          <p class="content" v-if="item.recordable_type=='payment_award'">充值赠送</p>
          <p class="content" v-if="item.recordable_type=='payment'">充值</p>
          <p class="content" v-if="item.recordable_type=='register'">新用户注册</p>
          <p class="date">{{item.created_at}}</p>
        </div>
        <div class="sum sup" v-if="item.coin>0">+{{item.coin}}</div>
        <div class="sum sub" v-if="item.coin<=0">{{item.coin}}</div>
      </div>
      <div class="noaddress" v-if="list.length==0">
        <p style="font-size:0.3rem;color:#999;margin-bottom:0.2rem;">你的钱包还是空的</p>
        <p style="font-size:0.26rem;color:#c5c5c5;">充值金币就能抓娃娃咯~</p>
      </div>
    </div>
    <recharge :show-recharge="showRecharge" source-class="my-coin" @cancel="hideModal" @success="getMyCoin"></recharge>
  </div>
</template>

<script>
import api from "@/assets/js/api";
import Recharge from "@/components/recharge";
import {updateAccount} from '@/assets/js/utils';//更新用户信息
export default {
  data() {
    return {
      coin: 0,
      list: [],
      showRecharge: false,
      page: 1,
      nonext: false
    };
  },

  components: { Recharge },

  computed: {},

  mounted() {
    this.getMyCoin();
    this.getlist();
  },

  methods: {
    getMyCoin() {
      updateAccount((res) => {
        this.coin = res.coin;
        this.page = 1;
        this.getlist();
        this.hideModal();
      })
    },
    showModal() {
      this.showRecharge = true;
    },
    hideModal() {
      this.showRecharge = false;
    },
    getlist () {
      var smData = {
        page: this.page,
        per_page: 15
      };
      api.fetch("/api/coin-records", { params: smData }).then(res => {
        if (res.meta.pagination.count > 0) {
          this.nonext = !res.meta.pagination.has_more;
          if (this.page == 1) {
            this.list = [];
          }
          if (this.list.length == 0) {
            this.list = res.data;
          } else {
            this.list = this.list.concat(res.data);
          }
        }
      });
    },
    getMore () {
      this.page++;
      this.getlist();
    }
  }
};
</script>
<style lang='less' scoped>
.my-coin {
  background-color: #fff;
  min-height: 100%;
  .noaddress {
    background: url("../../assets/images/plane.png") no-repeat 0.94rem 1.52rem;
    background-size: 4.99rem 2.67rem;
    text-align: center;
    padding-top: 5rem;
  }
.acount {
    height: 2rem;
    padding-top: 0.5rem;
    background-color: #fff;
    box-sizing: border-box;
    position: relative;
    border-bottom: 0.01rem solid #eee;
    .text,
    .num {
      text-align: center;
      color: #999;
      margin-bottom: 0.24rem;
      font-size: 0.26rem;
    }
    .num {
      font-size: 0.6rem;
      font-weight: bold;
      color: #ffb52b;
    }
    .go-charge {
      color:#09bb07;
      font-size: 0.24rem;
      position: absolute;
      right: 0.3rem;
      bottom: 0.52rem;
    }
  }
  .consume-list {
    .consume-item {
      justify-content: space-between;
      margin: 0 0.3rem;
      padding: 0.3rem 0;
      background: #fff;
      border-bottom: 0.01rem solid #eee;
      .content {
        font-size: 0.28rem;
        color: #333;
        margin-bottom: 0.12rem;
      }
      .date {
        font-size: 0.24rem;
        color: #999;
      }
      .sum {
        font-size: 0.28rem;
      }
      .sup {
        color: #09bb07;
      }
      .sub {
        color: #ffb52b;
      }
    }
  }
}
</style>
